package com.crr.mall.product.app;

import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import com.crr.mall.common.bean.ResultBean;
import com.crr.mall.common.utils.BaseController;
import com.crr.mall.product.query.SpuQuery;
import com.crr.mall.product.vo.SpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.crr.mall.product.entity.SpuEntity;
import com.crr.mall.product.service.SpuService;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.R;


/**
 * spu信息
 *
 * @author chenrenren
 * @email chenrenren@gmail.com
 * @date 2024-11-22 14:50:19
 */
@RestController
@RequestMapping("product/spu")
public class SpuController extends BaseController {
    @Autowired
    private SpuService spuService;

    @PostMapping("/{spuId}/up")
    public ResultBean skuUp(@PathVariable("spuId") Long spuId) {
     spuService.skuUp(spuId);
    return success("商品上架成功");
    }

    /**
     * 根据条件查询 spu信息
     *
     * @param spuQuery
     * @return
     */
    @PostMapping("/spuinfo/list")
    public ResultBean queryByAny(@RequestBody SpuQuery spuQuery) {
        PageUtils pageUtils = spuService.queryByCondition(BeanUtil.beanToMap(spuQuery));
        return success("spu信息列表", pageUtils);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SpuEntity spu = spuService.getById(id);

        return R.ok().put("spu", spu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResultBean save(@RequestBody SpuVO spuVO) {
        int i = spuService.saveDetail(spuVO);
        return saveResult(i);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuEntity spu) {
        spuService.updateById(spu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        spuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
